/**
 * 校验管控弹框
 */
import React, {Component} from 'react';
import {messages} from "share/common";
import {Button, Modal} from "antd";
import 'styles/financial-management/innvoice-bag-approve/components/verify-control-modal.scss'

class VerifyControlModal extends Component {

  type2Bg = {
    0: 'bg-warn',
    1: 'bg-error',
  };

  // 处理加签结果
  filterAddSignList = data => {
    const { successNum, successBusinessCodeList, failReason, checkResult } = data;
    const failNum = failReason ? Object.keys(failReason).length : 0;
    let list = [];
    successNum > 0 && list.push({
      title: messages('expense-15.key23',{arg1: successNum})/*{arg1}个单据处理成功*/,
      content: successBusinessCodeList.join(', ')
    });
    failNum > 0 && list.push({
      type: 1,
      title: messages('expense-15.key24',{arg1: failNum})/*{arg1}个单据处理失败*/,
      content: Object.keys(failReason).map(code => `${code} ${failReason[code]}`).join('\n')
    });
    checkResult && Object.keys(checkResult).map(code => {
      checkResult[code].map(item => {
        list.push({
          type: item.type,
          title: `${code} ${item.title}`,
          content: item.message
        })
      })
    });
    return list;
  };

  renderItem = item => (
    <div style={{ marginBottom: 16 }}>
      <div className="verify-control-modal-title">
        <div className={`verify-control-modal-title-bg ${this.type2Bg[item.type] || ''}`}/>
        <div className="verify-control-modal-title-text">{item.title}</div>
      </div>
      <div dangerouslySetInnerHTML={{ __html: item.content && item.content.replace(/\n/g, '<br/>') }}/>
    </div>
  );

  render() {
    const { visible, footer, checkResult } = this.props;
    return (
      <Modal
        title={messages('expense-15.key25')/*处理结果*/}
        visible={visible}
        maskClosable={false}
        className="verify-control-modal"
        footer={footer || [
          <Button onClick={() => this.props.onCancel(true)}>{messages('common.back')/*返回*/}</Button>,
        ]}
        onCancel={() => this.props.onCancel(true)}
      >
        {checkResult && this.filterAddSignList(checkResult).map(item => this.renderItem(item))}
      </Modal>
    );
  }
}

export default VerifyControlModal;
